/*
* 私募大赛 model
*privateGame.js*/

import {routerRedux} from 'dva/router';
import api from '../api';
import { notification,message } from 'antd';
import config from '../config/request';
import moment from 'moment';

notification.config({
	placement: 'topRight',
	duration: 5,
});
import Mock from 'mockjs';
export default {
	namespace : 'privateGame',
	state : {
		'gameName':'',//查询比赛名称
		'firstDate':'',
		'lastDate':'',
		'beginDate':'',
		'endDate':'',
		'RbeginDate':'',//排名
		'RendDate':'',
		'isShow':false,
		'qbeginDate':'',//查询比赛开始时间
		'qendDate':'',//查询比赛结束时间
		'isEdit':false,//基本信息是否编辑
		'marchName':'',//修改比赛名称
		'divisionName':'',//赛区名称
		'departmentIdList':[],//部门id
		'managerId':'',//管理人id
		'pdtIdArr':'',//产品id
		'enrollDivisionId':'',//报名部门id
		'divisionId':'',//排名选择赛区id
		'isCustom':false,//报名自定义
		'isShowDepartment':true,//是否展示部门选择
		'customDepartment':'',//自定义部门
		'BOfficeCode':'',//分公司名称
		'BDepartmentName':'',//营业部名称
		'BOfficeId':'',//分公司id
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({pathname, query}) => {
				if (pathname === '/match/matchList') {
						dispatch({type:'fundCompetition'})
				}
			})
		}
	},
	effects : {
		*alertError({payload}){
			console.log('>>>',payload)
			const openNotificationWithIcon = (type) => {
			  notification[type]({
			    message: payload.msg,
			  });
			};
			openNotificationWithIcon('error')
		},
		*changeState({payload},{call,put}){
			console.log('222',payload)
			yield put({
        type: 'setState',
        payload,
      });
		},
		*fundCompetition({payload},{call,put,select}){
			// const state=yield select(state => state.login);
			// console.log('sssdddff',state)
			const response = yield call(api.privateMgt.fundCompetition, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'fundCompetitionSuccess',
        payload: { response },
      });
      yield put({
      	type:'clearState'
      })
		},
		*addfundCompetition({payload},{call,put}){
			const response = yield call(api.privateMgt.addfundCompetition, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'fundCompetition',
      });
    },
    *editfundCompetition({payload},{call,put}){
			const id = payload.id
			const response = yield call(api.privateMgt.editfundCompetition,id, {...payload});
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'fundCompetition',
      });
      yield put({
        type: 'getGameDetails',
        payload:{
        	id:id
        }
      });
      yield put({
      	type:'setState',
      	payload:{
      		isEdit:false
      	}
      })
		},
    *deletefundCompetition({payload},{call,put}){
    	const id = payload.id;
			const response = yield call(api.privateMgt.deletefundCompetition, id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put(routerRedux.push({
				pathname:`/match/matchList`,
				query:{
				}
			}))
			yield put({
				type:'setState',
				payload:{
					isShow:false,
					'EnrollData':[],
					'departMentArr':[],
					'privateData':[],
					'beginDate':'',
					'endDate':'',
					'DetailsData':{},
					'marchName':''
				}
			})
    },
		*getGameDetails({payload},{call,put}){
			console.log('getGameDetails',payload)
			const id = payload.id
			const response = yield call(api.privateMgt.getGameDetails, id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			// yield put(routerRedux.push({
			// 	pathname:`/match/matchList`,
			// 	query:{
			// 		...payload,
			// 	}
			// }))
			yield put({
				type:'setState',
				payload:{
					isShow:true,
					isEdit:false,
					BDepartmentName:'',
					BOfficeCode:'',
					BOfficeId:'',
					RbeginDate:'',
					RendDate:'',
					beginDate:'',
					endDate:'',
				}
			})
			yield put({
        type: 'getGameDetailsSuccess',
        payload: { response },
      });
		},
		*getDepartment({payload},{call,put}){
			const response = yield call(api.privateMgt.getDepartment, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getDepartmentSuccess',
        payload: { response },
      });
		},
		*createDivision({payload},{call,put}){
			console.log('createDivision>',payload)
			yield put({
				type:'setState',
				payload:{
					'departmentIdList':[],
					'divisionName':[],
					// 'departMentArr':[]
				}
			})
			const response = yield call(api.privateMgt.createDivision, payload);
			console.log('createDivision>',response)
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			
			yield put({
        type: 'fundCompetition',
      });
			yield put({
				type:'getGameDetails',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'getEnrollGame',
				payload:{
					id:payload.competitionId
				}
			})
			
		},
		*editDivision({payload},{call,put,select}){
			
			const state=yield select(state => state.privateGame);
			console.log('editDivision>',payload,state)
			const id = payload.id
			const response = yield call(api.privateMgt.editDivision,id, {...payload});
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
				type:'getGameDetails',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'getEnrollGame',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'setState',
				payload:{
					'departmentIdList':[],
					'divisionName':[],
					// 'departMentArr':[]
				}
			})
		},
		*deleteDivision({payload},{call,put}){
			console.log('deleteDivision>',payload)
			const id = payload.divisionId
			const response = yield call(api.privateMgt.deleteDivision,id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
				type:'getGameDetails',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'getEnrollGame',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'setState',
				payload:{
					'departmentIdList':[],
					'divisionName':''
				}
			})
		},
		*deletEnrollePdt({payload},{call,put}){
			console.log('deletEnrollePdt>',payload)
			const competitionId = payload.competitionId;
			const id = payload.id;
			const response = yield call(api.privateMgt.deletEnrollePdt,competitionId,id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
				type:'getGameDetails',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'getEnrollGame',
				payload:{
					id:payload.competitionId
				}
			})
		},
		*getEnrollGame({payload},{call,put}){
			const id = payload.id
			const response = yield call(api.privateMgt.getEnrollGame, id);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getEnrollGameSuccess',
        payload: { response },
      });
		},
		*getPrivatePdt({payload},{call,put}){
			const response = yield call(api.privateMgt.getPrivatePdt, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getPrivatePdtSuccess',
        payload: { response },
      });
		},
		*getsmmcAlxr({payload},{call,put}){
			const response = yield call(api.privateMgt.getsmmcAlxr, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getsmmcAlxrSuccess',
        payload: { response },
      });
		},
		*gameSignUp({payload},{call,put}){
			yield put({
				type:'setState',
				payload:{
					'isCustom':false,
					'customDepartment':'',
					'Pdtdepartment':[],
					'pdtIdArr':'',
					'pdtData':[],
					'enrollDivisionId':'',
					'isShowDepartment':true,
					'divisionId':''
				}
			})
			console.log('gameSignUp',payload)
			const competitionId = payload.competitionId;
			const query = {}
				
			payload.departmentId&&(query.departmentId=payload.departmentId)
			payload.productId&&(query.productId=payload.productId)
			payload.description&&(query.description=payload.description)
			payload.divisionId&&(query.divisionId=payload.divisionId)
			const response = yield call(api.privateMgt.gameSignUp,competitionId,query);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
      	type: 'getEnrollGame',
      	payload:{
					id:payload.competitionId
				}
      })

		},
		*gamePublish({payload},{call,put}){
			const response = yield call(api.privateMgt.gamePublish, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'fundCompetition',
      });
      yield put({
      	type:'getGameDetails',
				payload:{
					id:payload.id
				}
      })
      yield put({
      	type: 'getEnrollGame',
      	payload:{
					id:payload.id
				}
      })
		},
		*getRankingsTable({payload},{call,put,select}){
			console.log('getRankingsTable>>',payload)
			// const state=yield select(state => state.privateGame);
			// const EnrollData = state.EnrollData;
   //    const BDepartmentName = state.BDepartmentName;
			const competitionId = payload.privateGame.DetailsData.id;
			const query={}
			payload.privateGame.divisionId&&(query.divisionId=payload.privateGame.divisionId)
			payload.privateGame.RbeginDate&&(query.beginDate=payload.privateGame.RbeginDate)
			payload.privateGame.RendDate&&(query.endDate=payload.privateGame.RendDate)
			if(payload.privateGame.BDepartmentName !==''&&payload.privateGame.BOfficeId!==''){
				query.departmentId=payload.privateGame.BDepartmentName
			}else if(payload.privateGame.BOfficeId!==''&&payload.privateGame.BDepartmentName ==''){
				query.departmentId=payload.privateGame.BOfficeId
			}else if(payload.privateGame.BOfficeId ==''&&payload.privateGame.BDepartmentName !==''){
				query.departmentId=payload.privateGame.BDepartmentName
			}
			
			
			console.log('getRankingsTable>>',query)
			const response = yield call(api.privateMgt.getRankingsTable, competitionId,query);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
        type: 'getRankingsTableSuccess',
        payload: { 
        	response,
        },
      });
		},
		*getPdtdepartment({payload},{call,put}){
			const response = yield call(api.privateMgt.getPdtdepartment, payload);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			console.log('getPdtdepartment>',response)
			yield put({
        type: 'getPdtdepartmentSuccess',
        payload: { response },
      });
		},
		*stopEntrollProduct({payload},{call,put}){
			console.log(222222,payload)
			const competitionId = payload.competitionId;
			const query={};
			query.id = payload.id;
			query.status = payload.status;
			const response = yield call(api.privateMgt.stopEntrollProduct, query);
			// if(response.code == '1'){
			// 	yield put({
	  //       type: 'alertError',
	  //       payload:{
	  //       	msg:response.msg,
	  //       }
	  //     });
			// }
			yield put({
				type:'getGameDetails',
				payload:{
					id:payload.competitionId
				}
			})
			yield put({
				type:'getEnrollGame',
				payload:{
					id:payload.competitionId
				}
			})
		},
	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload,
				
			}
		},
		clearState(state,{payload}){
			return {
				...state,
				...payload,
				'divisionName':'',
				'Pdtdepartment':[],
			}
		},
		getPrivatePdtSuccess(state,{payload}){
			const {data,page} = payload.response
      console.log("getPrivatePdtSuccess>>>",data);
      return {
        ...state,
        pdtData:data,
      }
		},
		getsmmcAlxrSuccess(state,{payload}){
			const {data,} = payload.response
      console.log("getsmmcAlxrSuccess>>>",data);
      return {
        ...state,
        privateData:data,
      }
		},
		fundCompetitionSuccess(state,{payload}){
			const {data} = payload.response
      console.log("fundCompetitionSuccess>>>",data);
      return {
        ...state,
        fundGameList:data,//比赛列表
      }
		},
		getGameDetailsSuccess(state,{payload}){
			const {data} = payload.response
      console.log("getDetailsSuccess>>>",data);
      // const departMentArr = [];
      // data.divisionList.map((item)=>{
      // 	for(let i=0;i<item.departmentList.length;i++){
      // 		departMentArr.push(item.departmentList[i])
      // 	}
      // })
      // console.log('getDetailsSuccess',departMentArr)
      return {
        ...state,
        DetailsData:data,//比赛详情
        marchName:data.name,
        firstDate:data.beginDate,
				lastDate:data.endDate,
				beginDate:data.beginDate,
				endDate:data.endDate,
				// departMentArr:departMentArr
      }
		},
		getDepartmentSuccess(state,{payload}){
			const {data} = payload.response
      console.log("getdepartmentSuccess>>>",data);
      return {
        ...state,
        departmentData:data,
      }
		},
		getEnrollGameSuccess(state,{payload}){
			const {data} = payload.response
      console.log("getEnrollGameSuccess>>>",data);
      return {
        ...state,
        EnrollData:data,
      }
		},
		getPdtdepartmentSuccess(state,{payload}){
			const {data} = payload.response
      console.log("getEnrollGameSuccess>>>",data);
      return {
        ...state,
        Pdtdepartment:data,
      }
		},
		getRankingsTableSuccess(state,{payload}){
			console.log('getRankingsTableSuccess>',state,payload)
			const {data} = payload.response
			
			console.log('getEnrollGameSuccess',data)
			return {
        ...state,
        ranksData:data,
      }
		}
		
	}
};
